
Ext.onReady(function(){
    var current_order_id; //当前选择的order的id
    var current_ticket_id; //当前选择的ticket的id
    var current_order_status; //当前order的状态
    var current_ticket_status; //当前ticket的状态
    var ordersrow;
    var ticketsrow;
	
    //############## 订单Grid ##############
    var app_orderCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id',
        hidden: true
    }, {
        header: '订单编号',
        dataIndex: 'order_no',
        width: 100,
        hidden: true
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '客户名称',
        dataIndex: 'customer',
        width: 70,
        hidden: true
    }, {
        header: '订单状态',
        dataIndex: 'status',
        width: 70
    }, {
        header: '总金额',
        dataIndex: 'price_sum',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '送票地址',
        dataIndex: 'send_address',
        width: 200
    }, {
        header: '送票时间',
        dataIndex: 'send_time',
        width: 50
    }, {
        header: '联系人',
        dataIndex: 'link_man',
        width: 70,
        hidden: true
    }, {
        header: '联系电话',
        dataIndex: 'link_tel',
        width: 100,
        hidden: true
    }, {
        header: '预定人',
        dataIndex: 'creator',
        width: 70
    }, {
        header: '预定时间',
        dataIndex: 'created_at',
        width: 120,
        renderer: Ext.util.Format.dateRenderer('Y-m-d H:i'),
        hidden: true
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 50
    }, {
        header: '客户要求',
        dataIndex: 'order_request',
        width: 120
    }]);

    app_orderCM.defaultSortable = true;
    var app_orderDS = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '/orders/getAppOrders'
        }),
        reader: orderReader
    });
    
    var app_orderGridPanel = new Ext.grid.GridPanel({
        el: 'app_ordersGrid',
        ds: app_orderDS,
        cm: app_orderCM,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 310,
        title: '订单信息'
    });
    
    app_orderGridPanel.render();
    app_orderDS.load();
    
    //双击订单查看机票详细
    app_orderGridPanel.on('rowdblclick', function(app_orderGridPanel, rowIndex, e){
        var current_order_selected = app_orderDS.data.items[rowIndex].id;
        ordersrow = app_orderDS.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        app_ticketDS.baseParams.id = current_order_id;
        app_ticketDS.reload();
    });
    //订单右键菜单
    app_orderGridPanel.on('rowcontextmenu', function(customergridpanel, rowIndex, e){
        var current_order_selected = app_orderDS.data.items[rowIndex].id;
        ordersrow = app_orderDS.getById(current_order_selected);//ds为绑定到grid的数据源
        current_order_id = ordersrow.get("id");
        current_order_status = ordersrow.get("status");
        e.preventDefault();
        rightClick.showAt(e.getXY());
    });
    
    //############ 订单-右键菜单 ##########
    var rightClick = new Ext.menu.Menu({
        items: [{
            handler: rightMenuFn,
            text: '【转为预订单】'
        }, {
            handler: rightMenu0Fn,
            text: '【提取PNR】'
        }, "-", {
            handler: rightMenu1Fn,
            text: '【修改订单】'
        }, {
            handler: rightMenu2Fn,
            text: '【取消订单】'
        }]
    });
    function rightMenuFn(){
        Ext.MessageBox.confirm('确认操作', '你确定将该订单转化为预订单,出票状态将变为确认出票?', changeAppToYDD);
    }
    function changeAppToYDD(btn){
        if (btn == 'yes') {
            Ext.Ajax.request({
                url: '/orders/changeAppToYDD',
                params: 'id=' + current_order_id,
                method: 'GET',
                success: function(result, request){
                    if (result.responseText.indexOf("成功") >= 0) {
                        //Ext.MessageBox.alert('操作成功', '操作成功');
                        app_ticketDS.baseParams.id = "";
                        app_ticketDS.reload();
                        app_orderDS.load();
                    }
                    else {
                        Ext.MessageBox.alert('操作失败', '' + result.responseText);
                    }
                },
                failure: function(result, request){
                    Ext.MessageBox.alert('操作失败', '转为预订单失败!');
                }
            });
        }
    }
    function rightMenu0Fn(){
        showPnrInfo(current_order_id, app_ticketDS);
    }
    function rightMenu1Fn(){
        ModOrder(app_orderDS, current_order_id, app_ticketDS);
    }
    function rightMenu2Fn(){
        var current_order_no = ordersrow.get("order_no");
        cancelOrder(current_order_status, current_order_id, current_order_no, app_orderDS, app_ticketDS);
    }
    
    //### 机票Grid ###
    var app_ticketCM = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), {
        header: '编号',
        dataIndex: 'id'
    }, {
        header: 'PNR',
        dataIndex: 'pnr',
        width: 50
    }, {
        header: '旅客姓名',
        dataIndex: 'passenger_name',
        width: 70
    }, {
        header: '证件号码',
        dataIndex: 'card',
        width: 140
    }, {
        header: '票号',
        dataIndex: 'ticket_no',
        width: 100
    }, {
        header: '状态',
        dataIndex: 'status',
        width: 70
    }, {
        header: '出票状态',
        dataIndex: 'status_type',
        sortable: true,
        width: 70
    }, {
        header: '航班号',
        dataIndex: 'air_no',
        width: 70
    }, {
        header: '航程',
        dataIndex: 'fromto',
        width: 70
    }, {
        header: '乘机日期',
        dataIndex: 'fly_date',
        width: 70
    }, {
        header: '起飞时间',
        dataIndex: 'fly_time',
        width: 70
    }, {
        header: '舱位',
        dataIndex: 'position',
        width: 50
    }, {
        header: '机票价格',
        dataIndex: 'price',
        width: 70
    }, {
        header: '机场费税',
        dataIndex: 'airdrome_tax',
        width: 70
    }, {
        header: '附加费',
        dataIndex: 'fuel_tax',
        width: 70
    }, {
        header: 'F',
        dataIndex: 'price_F',
        width: 50
    }, {
        header: '保险份数',
        dataIndex: 'insurance_number',
        width: 70
    }, {
        header: '保险F',
        dataIndex: 'insurance_F',
        width: 50
    }, {
        header: '其他',
        dataIndex: 'other',
        width: 50
    }, {
        header: '合计',
        dataIndex: 'total_price',
        width: 70
    
    }]);
    
    //隐藏ticket数据库id
    app_ticketCM.setHidden(1, !app_ticketCM.isHidden(1));
    var ticketProxy = new Ext.data.HttpProxy({
        url: '/orders/getTicketsByOrderID?showCancelTicket=no'
    });
    
    var app_ticketDS = new Ext.data.Store({
        proxy: ticketProxy,
        reader: ticketReader
    });
    
    var app_ticketGridPanel = new Ext.grid.GridPanel({
        el: 'app_ticketsGrid',
        ds: app_ticketDS,
        cm: app_ticketCM,
        loadMask: true,
        autoWidth: true,
        collapsible: true,
        animCollapse: true,
        height: 160,
        title: '航班信息'
    });
    
    app_ticketGridPanel.render();
    app_ticketDS.load();
    
    //############ 票-右键菜单 ##########
    function ticketRightMenu1(){
        ModTicket(app_ticketDS, current_ticket_id, app_orderDS);
    }
    function ticketRightMenu2(){
        var current_passenger_name = ticketsrow.get("passenger_name");
        cancelTicket(current_ticket_status, current_ticket_id, current_passenger_name, app_ticketDS);
    }
    var ticketRightClick = new Ext.menu.Menu({
        items: [{
            handler: ticketRightMenu1,
            text: '【编辑修改】'
        }, {
            handler: ticketRightMenu2,
            text: '【取消乘客】'
        }]
    });
    app_ticketGridPanel.on('rowcontextmenu', function(app_ticketGridPanel, rowIndex, e){
        var current_ticket_selected = app_ticketDS.data.items[rowIndex].id;
        ticketsrow = app_ticketDS.getById(current_ticket_selected);//ds为绑定到grid的数据源
        current_ticket_id = ticketsrow.get("id");
        current_ticket_status = ticketsrow.get("status");
        e.preventDefault();
        ticketRightClick.showAt(e.getXY());
    });
})
